/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package j_table;

import java.text.DecimalFormat;
import java.util.Random;
import java.util.Stack;

/**
 *
 * @author admin
 */
public class calc1 //自动出题并计算出答案的类
{

    Random r1=new Random();
    public Integer []a=new Integer[50];
    public Double []b=new Double[50];
    public calc1()
    {}
    public String admi(int range,int numb,int tips)//整数加减无括号
    {
        int n=numb;
        Integer ans=0;
        Integer num1,num2;
        String str1="";
        int i;
        for(i=0;i<n;i++)//5个数相加减
        {
            if(i==0)
            {
                num1=r1.nextInt(range);
                ans=num1;
                str1=new String(str1+num1);
            }
            else
            {
                num2=r1.nextInt(2);
                num1=r1.nextInt(range);
                if(num2==0)
                {
                    ans=ans+num1;
                    str1=new String(str1+"+"+num1);
                }
                else
                {
                    ans=ans-num1;
                    str1=new String(str1+"-"+num1);
                }
            }
        }
        //System.out.println(str1+"= "+ans);
        a[tips]=ans;
        return str1;
    }
    public String admix(int range,int numb,int digi,int tips)//小数加减无括号
    {
         DecimalFormat df;//
        int sRan=1;
        int n=numb;
        Double ans=0.0;
        Integer num1,num2,num4;
        Double num3,num5;
        String exSt;
        String str1="";
        int i;
        if(digi==1)//
            df = new DecimalFormat(".0");
        else if(digi==2)
            df = new DecimalFormat(".00");
        else
            df = new DecimalFormat(".000");//
        for(i=0;i<digi;i++)sRan=sRan*10;
        for(i=0;i<n;i++)//5个数相加减
        {
            if(i==0)
            {
                num1=r1.nextInt(range);
                exSt=""+num1;
                num3=Double.parseDouble(exSt);
                num4=r1.nextInt(sRan);
                exSt=""+num4;
                num5=Double.parseDouble(exSt);
                num3=num3+num5/sRan;
                str1=new String(str1+df.format(num3));//
                ans=num3;
            }
            else
            {
                num2=r1.nextInt(2);
                num1=r1.nextInt(range);
                exSt=""+num1;
                num3=Double.parseDouble(exSt);
                num4=r1.nextInt(sRan);
                exSt=""+num4;
                num5=Double.parseDouble(exSt);
                num3=num3+num5/sRan;
                if(num2==0)
                {
                    ans=ans+num3;
                    str1=new String(str1+"+"+df.format(num3));//
                }
                else
                {
                    ans=ans-num3;
                    str1=new String(str1+"-"+df.format(num3));//
                }
            }
        }
        //System.out.println(str1+"= "+ans);
        b[tips]=ans;
        return str1;
    }
    public String kammd(int range,int numb,int tips)//整数加减乘除有括号
    {
        boolean flag=false;
        int j=0;
        int n=numb;
        Double temp1;
        Stack<Double> std1 = new Stack();
        Stack<Integer> stc1=new Stack();
        String exSt;
        Integer num1,num2;
        Double num3=0.0;
        String str1="";
        for(int i=0;i<n;)
        {
            if(i==0)
            {
                if(r1.nextInt(100)<25)
                {
                    stc1.push(6);
                    flag=true;
                    str1=new String(str1+"(");
                    j=0;
                }
                num1=r1.nextInt(range)+1;
                str1=new String(str1+num1);
                exSt=""+num1;
                num3=Double.parseDouble(exSt);
                std1.push(num3);
                i++;
            }
            else{
                    while(true)
                    {
                        num2=r1.nextInt(5);
                        if(num2==4)
                        {
                            if(flag==true&&j>0)break;
                            else continue;
                        }
                        else
                        {
                            j++;
                            break;
                        }
                    }
                    if(num2==4)
                    {
                        str1=new String(str1+")");
                        while(stc1.peek()!=6)
                        {
                            switch(stc1.pop())
                            {
                                case 0:temp1=std1.pop();temp1=std1.pop()+temp1;std1.push(temp1);break;
                                case 1:temp1=std1.pop();temp1=std1.pop()-temp1;std1.push(temp1);break;
                            }
                        }
                        stc1.pop();
                        flag=false;
                        j=0;
                        if(!stc1.empty())
                            if(stc1.peek()==2||stc1.peek()==3)
                            {
                                switch(stc1.pop())
                                {
                                    case 3:temp1=std1.pop();temp1=std1.pop()*temp1;std1.push(temp1);break;
                                    case 4:temp1=std1.pop();temp1=std1.pop()/temp1;std1.push(temp1);break;
                                }
                            }
                    }
                    else
                    {
                        switch(num2){
                                case 0:str1=new String(str1+"+");break;
                                case 1:str1=new String(str1+"-");break;
                                case 2:str1=new String(str1+"*");break;
                                case 3:str1=new String(str1+"/");break;
                        }
                        if(!flag&&r1.nextInt(100)<25&&i<=n-2)
                        {
                            stc1.push(num2);
                            stc1.push(6);
                            j=0;
                            flag=true;
                            str1=new String(str1+"(");
                            num1=r1.nextInt(range)+1;
                            str1=new String(str1+num1);
                            exSt=""+num1;
                            num3=Double.parseDouble(exSt);
                            std1.push(num3);
                            i++;
                        }
                        else
                        {
                            num1=r1.nextInt(range)+1;
                            exSt=""+num1;
                            num3=Double.parseDouble(exSt);
                            switch(num2){
                                    case 0:
                                    case 1:if(stc1.empty()||stc1.peek()==6){stc1.push(num2);std1.push(num3);break;}else{
                                        switch(stc1.pop())
                                        {
                                            case 0:temp1=std1.pop();temp1=std1.pop()+temp1;std1.push(temp1);std1.push(num3);stc1.push(num2);break;
                                            case 1:temp1=std1.pop();temp1=std1.pop()-temp1;std1.push(temp1);std1.push(num3);stc1.push(num2);break;
                                        }
                                    }break;
                                    case 2:temp1=std1.pop();temp1=temp1*num1;std1.push(temp1);break;
                                    case 3:temp1=std1.pop();temp1=temp1/num1;std1.push(temp1);break;
                            }
                            str1=new String(str1+num1);
                            i++;
                        }
                    }
                }
        }
        if(flag)
        {
                        str1=new String(str1+")");
                        while(stc1.peek()!=6)
                        {
                            switch(stc1.pop())
                            {
                                case 0:temp1=std1.pop();temp1=std1.pop()+temp1;std1.push(temp1);break;
                                case 1:temp1=std1.pop();temp1=std1.pop()-temp1;std1.push(temp1);break;
                            }
                        }
                        stc1.pop();
                        flag=false;
                        j=0;
                        if(!stc1.empty())
                            if(stc1.peek()==2||stc1.peek()==3)
                            {
                                switch(stc1.pop())
                                {
                                    case 3:temp1=std1.pop();temp1=std1.pop()*temp1;std1.push(temp1);break;
                                    case 4:temp1=std1.pop();temp1=std1.pop()/temp1;std1.push(temp1);break;
                                }
                            }
        }
        while(!stc1.empty())
        {
            switch(stc1.pop())
            {
                case 0:temp1=std1.pop();temp1=std1.pop()+temp1;std1.push(temp1);break;
                case 1:temp1=std1.pop();temp1=std1.pop()-temp1;std1.push(temp1);break;
            }
        }
        b[tips]=std1.pop();
        //System.out.println(str1+"= "+std1.pop());
        return str1;
    }
    public String kammdx(int range,int numb,int digi,int tips)//小数加减乘除有括号
    {
        DecimalFormat df;//
        int sRan=1;
        boolean flag=false;
        int j=0;
        int n=numb;
        Double temp1;
        Stack<Double> std1 = new Stack();
        Stack<Integer> stc1=new Stack();
        String exSt;
        Integer num1,num2,num4;
        Double num3=0.0,num5;
        String str1="";
         if(digi==1)//
            df = new DecimalFormat(".0");
        else if(digi==2)
            df = new DecimalFormat(".00");
        else
            df = new DecimalFormat(".000");//
        for(int i=0;i<digi;i++)
        {
            sRan=sRan*10;
        }
        for(int i=0;i<n;)
        {
            if(i==0)
            {
                if(r1.nextInt(100)<25)
                {
                    stc1.push(6);
                    flag=true;
                    str1=new String(str1+"(");
                    j=0;
                }
                num1=r1.nextInt(range)+1;
                exSt=""+num1;
                num3=Double.parseDouble(exSt);
                num4=r1.nextInt(sRan);
                exSt=""+num4;
                num5=Double.parseDouble(exSt);
                num3=num3+num5/sRan;
                std1.push(num3);
               str1=new String(str1+df.format(num3));//
                i++;
            }
            else{
                    while(true)
                    {
                        num2=r1.nextInt(5);
                        if(num2==4)
                        {
                            if(flag==true&&j>0)break;
                            else continue;
                        }
                        else
                        {
                            j++;
                            break;
                        }
                    }
                    if(num2==4)
                    {
                        str1=new String(str1+")");
                        while(stc1.peek()!=6)
                        {
                            switch(stc1.pop())
                            {
                                case 0:temp1=std1.pop();temp1=std1.pop()+temp1;std1.push(temp1);break;
                                case 1:temp1=std1.pop();temp1=std1.pop()-temp1;std1.push(temp1);break;
                            }
                        }
                        stc1.pop();
                        flag=false;
                        j=0;
                        if(!stc1.empty())
                            if(stc1.peek()==2||stc1.peek()==3)
                            {
                                switch(stc1.pop())
                                {
                                    case 3:temp1=std1.pop();temp1=std1.pop()*temp1;std1.push(temp1);break;
                                    case 4:temp1=std1.pop();temp1=std1.pop()/temp1;std1.push(temp1);break;
                                }
                            }
                    }
                    else
                    {
                        switch(num2){
                                case 0:str1=new String(str1+"+");break;
                                case 1:str1=new String(str1+"-");break;
                                case 2:str1=new String(str1+"*");break;
                                case 3:str1=new String(str1+"/");break;
                        }
                        if(!flag&&r1.nextInt(100)<25&&i<=n-2)
                        {
                            stc1.push(num2);
                            stc1.push(6);
                            j=0;
                            flag=true;
                            str1=new String(str1+"(");
                            num1=r1.nextInt(range)+1;
                            exSt=""+num1;
                            num3=Double.parseDouble(exSt);
                            num4=r1.nextInt(sRan);
                            exSt=""+num4;
                            num5=Double.parseDouble(exSt);
                            num3=num3+num5/sRan;
                            std1.push(num3);
                            str1=new String(str1+df.format(num3));//
                            i++;
                        }
                        else
                        {
                            num1=r1.nextInt(range)+1;
                            exSt=""+num1;
                            num3=Double.parseDouble(exSt);
                            num4=r1.nextInt(sRan);
                            exSt=""+num4;
                            num5=Double.parseDouble(exSt);
                            num3=num3+num5/sRan;
                            switch(num2){
                                    case 0:
                                    case 1:if(stc1.empty()||stc1.peek()==6){stc1.push(num2);std1.push(num3);break;}else{
                                        switch(stc1.pop())
                                        {
                                            case 0:temp1=std1.pop();temp1=std1.pop()+temp1;std1.push(temp1);std1.push(num3);stc1.push(num2);break;
                                            case 1:temp1=std1.pop();temp1=std1.pop()-temp1;std1.push(temp1);std1.push(num3);stc1.push(num2);break;
                                        }
                                    }break;
                                    case 2:temp1=std1.pop();temp1=temp1*num3;std1.push(temp1);break;
                                    case 3:temp1=std1.pop();temp1=temp1/num3;std1.push(temp1);break;
                            }
                            str1=new String(str1+df.format(num3));//
                            i++;
                        }
                    }
                }
        }
        if(flag)
        {
                        str1=new String(str1+")");
                        while(stc1.peek()!=6)
                        {
                            switch(stc1.pop())
                            {
                                case 0:temp1=std1.pop();temp1=std1.pop()+temp1;std1.push(temp1);break;
                                case 1:temp1=std1.pop();temp1=std1.pop()-temp1;std1.push(temp1);break;
                            }
                        }
                        stc1.pop();
                        flag=false;
                        j=0;
                        if(!stc1.empty())
                            if(stc1.peek()==2||stc1.peek()==3)
                            {
                                switch(stc1.pop())
                                {
                                    case 3:temp1=std1.pop();temp1=std1.pop()*temp1;std1.push(temp1);break;
                                    case 4:temp1=std1.pop();temp1=std1.pop()/temp1;std1.push(temp1);break;
                                }
                            }
        }
        while(!stc1.empty())
        {
            switch(stc1.pop())
            {
                case 0:temp1=std1.pop();temp1=std1.pop()+temp1;std1.push(temp1);break;
                case 1:temp1=std1.pop();temp1=std1.pop()-temp1;std1.push(temp1);break;
            }
        }
        b[tips]=std1.pop();
        //System.out.println(str1+"= "+std1.pop());
        return str1;
    }
    public String ammd(int range,int numb,int tips)//整数加减乘除无括号
    {
        int n=numb;
        Double temp1;
        Stack<Double> std1 = new Stack();
        Stack<Integer> stc1=new Stack();
        String exSt;
        Integer num1,num2;
        Double num3=0.0;
        String str1="";
        for(int i=0;i<n;i++)
        {
            if(i==0)
            {
                num1=r1.nextInt(range)+1;
                str1=new String(str1+num1);
                exSt=""+num1;
                num3=Double.parseDouble(exSt);
                std1.push(num3);
            }
            else{
                    num2=r1.nextInt(4);
                    num1=r1.nextInt(range)+1;
                    exSt=""+num1;
                    num3=Double.parseDouble(exSt);
                    switch(num2){
                            case 0:str1=new String(str1+"+");
                            case 1:if(num2==1)str1=new String(str1+"-");if(stc1.empty()){stc1.push(num2);std1.push(num3);break;}else{
                                switch(stc1.pop())
                                {
                                    case 0:temp1=std1.pop();temp1=std1.pop()+temp1;std1.push(temp1);std1.push(num3);stc1.push(num2);break;
                                    case 1:temp1=std1.pop();temp1=std1.pop()-temp1;std1.push(temp1);std1.push(num3);stc1.push(num2);break;
                                }
                            }break;
                            case 2:str1=new String(str1+"*");temp1=std1.pop();temp1=temp1*num1;std1.push(temp1);break;
                            case 3:str1=new String(str1+"/");temp1=std1.pop();temp1=temp1/num1;std1.push(temp1);break;
                    }
                    str1=new String(str1+num1);
                }
        }
        while(!stc1.empty())
        {
            switch(stc1.pop())
            {
                case 0:temp1=std1.pop();temp1=std1.pop()+temp1;std1.push(temp1);break;
                case 1:temp1=std1.pop();temp1=std1.pop()-temp1;std1.push(temp1);break;
            }
        }
        b[tips]=std1.pop();
        //System.out.println(str1+"= "+std1.pop());
        return str1;
    }
    public String ammdx(int range,int numb,int digi,int tips)//小数加减乘除无括号
    {
        DecimalFormat df;//
        int sRan=1;
        int n=numb;
        Double temp1;
        Stack<Double> std1 = new Stack();
        Stack<Integer> stc1=new Stack();
        String exSt;
        Integer num1,num2,num4;
        Double num3=0.0,num5;
        String str1="";
         if(digi==1)//
            df = new DecimalFormat(".0");
        else if(digi==2)
            df = new DecimalFormat(".00");
        else
            df = new DecimalFormat(".000");//
        for(int i=0;i<digi;i++)sRan=sRan*10;
        for(int i=0;i<n;i++)
        {
            if(i==0)
            {
                num1=r1.nextInt(range)+1;
                exSt=""+num1;
                num3=Double.parseDouble(exSt);
                num4=r1.nextInt(sRan);
                exSt=""+num4;
                num5=Double.parseDouble(exSt);
                num3=num3+num5/sRan;
                std1.push(num3);
                str1=new String(str1+df.format(num3));//
            }
            else{
                    num2=r1.nextInt(4);
                    num1=r1.nextInt(range)+1;
                    exSt=""+num1;
                    num3=Double.parseDouble(exSt);
                    num4=r1.nextInt(sRan);
                    exSt=""+num4;
                    num5=Double.parseDouble(exSt);
                    num3=num3+num5/sRan;
                    switch(num2){
                            case 0:str1=new String(str1+"+");
                            case 1:if(num2==1)str1=new String(str1+"-");if(stc1.empty()){stc1.push(num2);std1.push(num3);break;}else{
                                switch(stc1.pop())
                                {
                                    case 0:temp1=std1.pop();temp1=std1.pop()+temp1;std1.push(temp1);std1.push(num3);stc1.push(num2);break;
                                    case 1:temp1=std1.pop();temp1=std1.pop()-temp1;std1.push(temp1);std1.push(num3);stc1.push(num2);break;
                                }
                            }break;
                            case 2:str1=new String(str1+"*");temp1=std1.pop();temp1=temp1*num3;std1.push(temp1);break;
                            case 3:str1=new String(str1+"/");temp1=std1.pop();temp1=temp1/num3;std1.push(temp1);break;
                    }
                     str1=new String(str1+df.format(num3));//
                }
        }
        while(!stc1.empty())
        {
            switch(stc1.pop())
            {
                case 0:temp1=std1.pop();temp1=std1.pop()+temp1;std1.push(temp1);break;
                case 1:temp1=std1.pop();temp1=std1.pop()-temp1;std1.push(temp1);break;
            }
        }
        b[tips]=std1.pop();
        //System.out.println(str1+"= "+std1.pop());
        return str1;
    }
}

